# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Carlo Landmeter <clandmeter@gmail.com>
pkgname=lua-sqlite
_rockname=lsqlite3
pkgver=0.9.5
_uuid="fsl_9y"
[ "$pkgver" != "0.9.5" ] && die "Please update uuid!"
_luaversions="5.1 5.2 5.3"
pkgrel=2
pkgdesc="Sqlite3 Binding for Lua"
url="http://lua.sqlite.org/index.cgi/home"
arch="all"
license="MIT"
depends=""
makedepends="sqlite-dev"
install=""
subpackages=""
# the uuid determines the archive version and the filename the target directory
source="$pkgname-$pkgver.zip::http://lua.sqlite.org/index.cgi/zip/$pkgname-$pkgver.zip?uuid=$_uuid"

builddir="$srcdir"/$pkgname-$pkgver

for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_package"
done

prepare() {
	default_prepare || return 1

	local lver
	for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver" || return 1
	done
}

build() {
	local lver
	for lver in $_luaversions; do
		cd "$builddir-$lver"
		msg "Building for Lua $lver..."
		${CC:-gcc} ${CFLAGS} $(pkg-config --cflags sqlite3) \
			$(pkg-config --cflags lua$lver) -shared -fPIC \
			-DLSQLITE_VERSION=\"$pkgver\" -o lsqlite3.so lsqlite3.c \
			$(pkg-config --libs sqlite3) || return 1
	done
}

package() {
	mkdir -p "$pkgdir"
}

_package() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"

	cd "$builddir-$lver"
	install -Dm755 lsqlite3.so \
		"$subpkgdir"/usr/lib/lua/$lver/lsqlite3.so || return 1

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="3d66a0e7250bada97f753c8204f6ca27142c2c8d3b4a3fba433200f5a90726f4b8ba7317886e11b819ac10bc3c002470e3039509870d0ebd9ff3835d3cff53e5  lua-sqlite-0.9.5.zip"
